package com.qf.fcy.youshare;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import com.qf.fcy.youshare.adapter.CommonPagerAdapter;
import com.qf.fcy.youshare.adapter.EnterAdapter;
import com.qf.fcy.youshare.utils.CommonUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener,
        AdapterView.OnItemClickListener {
    // 上次点击Back的时间
    private long lastBackTime;
    private RadioGroup rg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setIcon(new ColorDrawable());
            actionBar.setCustomView(R.layout.action_bar_main_title);
        }

        rg = (RadioGroup) findViewById(R.id.main_rg);
        RadioButton rb = (RadioButton) rg.getChildAt(0);
        rb.setChecked(true);

        ViewPager pager = (ViewPager) findViewById(R.id.main_pager_poster);
        // TODO 设置Adapter
        List<Integer> list = new LinkedList<Integer>();
        list.add(R.drawable.image1);
        list.add(R.drawable.image2);
        list.add(R.drawable.image3);
        list.add(R.drawable.image4);
        list.add(R.drawable.image5);
        pager.setAdapter(new CommonPagerAdapter(this, list));
        pager.setOnPageChangeListener(this);

        // TODO 功能模块九宫格
        GridView gridView = (GridView) findViewById(R.id.main_entry);
        List<Map<String, Object>> data = new LinkedList<Map<String, Object>>();
        int[] itemBg = {
                R.drawable.ico_yingshi_bg, R.drawable.ico_shangcheng_bg,
                R.drawable.ico_xianchang_bg, R.drawable.ico_shejiao_bg,
                R.drawable.ico_user_bg, R.drawable.ico_setting_bg
        };
        String[] itemTitles = {"影视", "商城", "现场", "社交", "个人中心", "设置"};
        int[] itemIconIds = {
                R.drawable.ico_yingshi, R.drawable.ico_shangcheng,
                R.drawable.ico_xianchang, R.drawable.ico_shejiao,
                R.drawable.ico_user, R.drawable.ico_setting
        };
        for (int i = 0; i < itemTitles.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("itemBg", itemBg[i]);
            map.put("itemTitle", itemTitles[i]);
            map.put("itemIconId", itemIconIds[i]);
            data.add(map);
        }

        gridView.setAdapter(new EnterAdapter(this, data));
        gridView.setOnItemClickListener(this);

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton rb = (RadioButton) rg.getChildAt(position);
        rb.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    /**
     * 当前Activity显示的时候，按下Back键时回调
     */
    @Override
    public void onBackPressed() {
        // 获取当前的毫秒数，和上次点击的时间进行比较，如果相差2s，则退出
        long currBackTime = System.currentTimeMillis();
        if (currBackTime - lastBackTime < 2000) {
            // TODO 结束程序
            // 利用 super.onBackPressed() 让Activity 自己处理返回键的
            // 或者用 finish()结束
//            super.onBackPressed();
            finish();
        } else {
            lastBackTime = currBackTime;
            CommonUtils.toast(this, "再次点击退出");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = null;
        switch (position) {
            case 1: // 跳转商城
                intent = new Intent(this,StoreActivity.class);
                break;
            case 3: // 社交
                intent = new Intent(this,WebViewActivity.class);
                break;
            case 4: // 设置
                intent = new Intent(this,SettingActivity.class);
                break;
        }

        if (intent != null) {
            startActivity(intent);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
